package com.dhgate.spu.analysis.stat;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhgate.spu.analysis.consts.CommonConsts;
import com.dhgate.spu.analysis.consts.SysConsts;
import com.dhgate.spu.analysis.init.InitParameter;
/**
 * 
 * @Description: 统计被归总的itemcode  和  没有被归总的itemcode ，
 * 分别输出到文件spu_itemcode_xxid.txt  nospu_itemcode_xxid.txt
 * @author  
 * @version 1.0
 * @create time 2014-3-28 下午4:58:44
 */
public class StatItemcodeMain {
	private static final Logger logger = LoggerFactory.getLogger(StatItemcodeMain.class);

	//100万
	private static final int SOLRQUERY_ROWS=1000000;
	/**
	 * 
	 * @Description: TODO
	 *
	 * @author zhaowei
	 * @param args
	 * @throws Exception void
	 * @create time 2014-3-28 下午4:58:40
	 */
	public static void main(String[] args) throws Exception {
		if(args.length<1){
			throw new Exception("input prodlineid is null");
		}
		// 初始化基本配置参数
		InitParameter.init();
		//统计itemcode
		countItemcode(args[0]);

	}

	private static void countItemcode(String prodLineId) {
		logger.info("begin stat itemcode...........");
		try {
			//*************统计已归总的itemcode***********************
			SolrQuery solrQuery = new SolrQuery();
			solrQuery.setQuery("*:*");
			solrQuery.setRows(SOLRQUERY_ROWS);
			solrQuery.addFilterQuery("prodlineid:" + prodLineId);
			solrQuery.addFilterQuery("-" + CommonConsts.ExtFieldName.SPU_STANDARD_ID + ":nohave");
			solrQuery.addSort(CommonConsts.ExtFieldName.SPU_STANDARD_ID,ORDER.asc);
			QueryResponse qr = SysConsts.SOLR_SERVER.query(solrQuery);
			//本地调试可以打开这个
//			QueryResponse qr =new HttpSolrServer("http://172.21.50.38:9600/search/spu").query(solrQuery);
			logger.info("qr=" + qr);
			long num = qr.getResults().getNumFound();
			StringBuffer spuSB = new StringBuffer();
			String spu_standard_id="1";
//			spuSB.append("归总为spu的产品数量:"+num).append("\n");
			spuSB.append("SpuStandard").append(spu_standard_id).append("\n");
			for (SolrDocument doc : qr.getResults()) {
				String ssi=doc.getFieldValue(CommonConsts.ExtFieldName.SPU_STANDARD_ID).toString();
				if(!spu_standard_id.equals(ssi)){
					spu_standard_id=ssi;
					spuSB.append("SpuStandard").append(spu_standard_id).append("\n");
				}
				spuSB.append(doc.getFieldValue("itemcode")).append("\n");
			}
			File file = new File("spu_itemcode_"+prodLineId+".txt");
			if(file.exists()){
				FileUtils.deleteQuietly(file);
			}
			FileUtils.writeStringToFile(file, spuSB.toString());

			
			
			//*************统计没有归总的itemcode************************
			solrQuery.setFilterQueries(CommonConsts.ExtFieldName.SPU_STANDARD_ID + ":nohave");
			solrQuery.addFilterQuery("prodlineid:" + prodLineId);
			QueryResponse nohaveQr = SysConsts.SOLR_SERVER.query(solrQuery);
			//QueryResponse nohaveQr = new HttpSolrServer("http://172.21.50.38:9600/search/spu").query(solrQuery);
			long nohaveNum = nohaveQr.getResults().getNumFound();
			StringBuffer noSpuSB = new StringBuffer();
//			noSpuSB.append("没有归总为spu的产品数量:"+nohaveNum).append("\n");
			for (SolrDocument doc : nohaveQr.getResults()) {
				String ic = doc.get("itemcode").toString();
				String productname = doc.get("productname").toString();
				String sp = "\1";
				noSpuSB.append(ic).append(sp).append(productname).append("\n");
			}
			System.out.println(noSpuSB.toString());
			File nospuFile = new File("nospu_itemcode_"+prodLineId+".txt");
			if(file.exists()){
				FileUtils.deleteQuietly(nospuFile);
			}
			FileUtils.writeStringToFile(nospuFile, noSpuSB.toString());
			logger.info("end stat itemcode...........");
		} catch (SolrServerException e) {
			logger.error("stat itemcode failed" + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			logger.error("stat itemcode failed" + e.getMessage());
			e.printStackTrace();
		}

	}

}
